package example2;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.util.TablesNamesFinder;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringTest {


    @Test
    public void t1() throws Exception {

        InputStream stream = new ClassPathResource("file.txt").getStream();
        String text = IOUtils.toString(stream, "UTF-8");
        System.out.println(text);

        Select select = (Select) CCJSqlParserUtil.parse(text);
        TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
        List<String> tableList = tablesNamesFinder.getTableList(select);
        System.out.println(tableList);


//        Pattern pattern = Pattern.compile("FROM/s+/S+/.(/S+)/s+");
//        Matcher matcher = pattern.matcher(text);
//        if (matcher.find()) {
//            String group = matcher.group(1);
//            System.out.println(group);
//        }

    }


    @Test
    public void t2() throws Exception {

        String text = "DELETE ; INSERT INTO () ,SELECT * FORM";
        System.out.println(text);

        int index = text.indexOf("SELECT");
        if (-1 == index) {
            index = text.indexOf("select");
        }

        String s1 = text.substring(0, index);
        String s2 = text.substring(index);


        System.out.println("");

        FileUtils.readFileToString(new File(""), "");

        FileUtils.writeStringToFile(new File(""), "", "");
    }

    @Test
    public void t3() throws Exception {
        String sql = "FileUtils.writeStringToFile_SELECT_* FROM DUAL";
        int select = sql.indexOf("SELECT");
        String substring = sql.substring(select);
        String substring1 = sql.substring(0, select);
        System.out.println();
    }

    @Test
    public void t4() throws Exception {

        File file = FileUtils.getFile("C:/Users/yangyang/Desktop/qscilexer类名.txt");
        List<String> list = FileUtils.readLines(file, "UTF-8");

        for (String line : list) {
            String right = line.substring(9);
            String left = line.substring(0, 9);
            String classNane = left + StrUtil.upperFirst(right);
            ;
            String format = String.format("else if (suffix == \"%s\") { lexer = new %s; }", right.toUpperCase(), classNane);
            System.out.println(format);
        }

    }

    @Test
    public void t5() throws Exception {
        File file = FileUtils.getFile("C:/Users/yangyang/Desktop/二维码最大容量.TXT");
        File file2 = FileUtils.getFile("C:/Users/yangyang/Desktop/最大长度二维码2.txt");
        String s = FileUtils.readFileToString(file, "GBK");
        String s2 = FileUtils.readFileToString(file2, "GBK");
//        System.out.println(s.length());
//        System.out.println(s2.length());

        List<String> strings = splitByByteSize(s, 2951);
    }

    /**
     * 根据字符串指定的字节大写拆分
     *
     * @param s        字符串
     * @param byteSize 字节大小
     * @return 字符串列表
     */
    static List<String> splitByByteSize(String s, int byteSize) throws UnsupportedEncodingException {
        char[] chars = s.toCharArray();
        String[] strArr = new String[chars.length];
        for (int i = 0; i < chars.length; i++) strArr[i] = String.valueOf(chars[i]);
        ArrayList<Integer> indexList = new ArrayList<>();

        int groupLen = 0;
        indexList.add(0);
        for (int i = 0; i < strArr.length; i++) {
            String str = strArr[i];
            int tmp = groupLen + str.getBytes("GBK").length;
            if (tmp > byteSize) {
                indexList.add(i - 1);
                groupLen = str.getBytes().length;
            } else if (tmp == byteSize) {
                indexList.add(i);
                groupLen = str.getBytes().length;
            } else {
                groupLen = tmp;
            }
        }
        Integer integer = indexList.get(indexList.size() - 1);
        if (integer != strArr.length) indexList.add(strArr.length - 1);

        List<String> list = new ArrayList<>();
        for (int i = 1; i < indexList.size(); i++) {
            String[] copyOfRange = Arrays.copyOfRange(strArr, indexList.get(i - 1), indexList.get(i));
            String join = StringUtils.join(copyOfRange, "");
            list.add(join);
        }
        return list;
    }


    @Test
    public void t6() throws Exception {
        int v = 4;
        String format = String.format("%03d", v);
        System.out.println(format);
    }

}
